
from machine import I2C,Pin
import time
from neopixel import NeoPixel
from ssd1306 import SSD1306_I2C

i2c = I2C(0,scl=Pin(2),sda=Pin(3),freq=400000)
oled = SSD1306_I2C(128,64,i2c,0x3c)

# 定义全局变量
WS2812_NUM = 1
WS2812_LED_LEVEL = 0x10
G = 0xff
R = 0
B = 0

pin = Pin(8, Pin.OUT)   #配置GPIO8为输出端口，控制ws2812灯珠的DIN信号
np = NeoPixel(pin, WS2812_NUM,3,1)

def ws2812_LED_On(phase):
    global G,R,B
    
    for i in range(WS2812_NUM - 1):
        np[WS2812_NUM -1 - i] = np[WS2812_NUM - 2 - i]
        
    if phase < WS2812_LED_LEVEL:
        G = int(G - (0xff / WS2812_LED_LEVEL))
        R = int(R + (0xff / WS2812_LED_LEVEL))
        phase += 1
        if phase == WS2812_LED_LEVEL:
            G = 0
            R = 0xff
    elif phase < (WS2812_LED_LEVEL * 2):
        R = int(R - (0xff / WS2812_LED_LEVEL))
        B = int(B + (0xff / WS2812_LED_LEVEL))
        phase += 1        
        if phase == (WS2812_LED_LEVEL * 2):
            R = 0
            B = 0xff
    elif  phase < (WS2812_LED_LEVEL * 3):
        B = int(B - (0xff / WS2812_LED_LEVEL))
        G = int(G + (0xff / WS2812_LED_LEVEL))
        phase += 1        
        if phase == (WS2812_LED_LEVEL * 3):
            phase = 0
            B = 0
            G = 0xff
    np[0] = (G,R,B)
    return phase

utf_8_to_gb2312 = {'深':0xC9EE,'圳':0xDBDA,'源':0xD4B4,'悦':0xD4C3,'科':0xBFC6,
                   '技':0xBCBC,'有':0xD3D0,'限':0xCFDE,'公':0xB9AB,'司':0xCBBE,
                   '温':0xCEC2,'湿':0xCAAA,'度':0xB6C8,'显':0xCFD4,'示':0xCABE,
                   '天':0xCCEC,'气':0xC6F8,'时':0xCAB1,'间':0xBCE4,'日':0xC8D5,'期':0xC6DA,'啊':0xB0A1}
HZFontInfo = [0xa1a1,94,0xb0a1,94,0x8140,190,0xaa40,96,0xa840,96]
HZFontAddr = [0,0x7980,0x3C780,0x6BF80,0xABB80]
#GB18030汉字分5个区，每个区对应2个元素，第一个对应的是该区的起始地址，
#第二个对应的是起始地址第二个字节最大值与最小值的差 + 1


import framebuf
def CalcGBSection(incode):
    highbyte = (incode >> 8) & 0xff
    lowbyte = incode & 0xff
    section = 0xff
    if ((highbyte >= 0x81) and (lowbyte >= 0x40)):
        if (highbyte < 0xa1):
            section = 2
        elif ((highbyte >= 0xa1) and (highbyte <= 0xab) and (lowbyte >= 0xa1)):
            section = 0
        elif ((highbyte >= 0xa8) and (highbyte <= 0xa9)):
            section = 4
        elif ((highbyte >= 0xaa) and (highbyte <= 0xfe) and (lowbyte <= 0xa0)):
            section = 3
        elif ((highbyte >= 0xb0) and (highbyte <= 0xf7) and (lowbyte >= 0xa1)):
            section = 1
    return section
        
        
        

def show_chinese(text,x_start,y_start,color):
    with open('GB18030P.BIN','rb') as f:
        for char in text :
            gb_code = utf_8_to_gb2312[char]
#            print(char)
#            print(hex(gb_code))

            section = CalcGBSection(gb_code)
#           print (section)
            gb_code -= HZFontInfo[section * 2]
#            print (gb_code)
            hbyte = (gb_code >> 8) & 0xff
            lbyte = gb_code & 0xff
            if ((section >= 2) and (lbyte > (0x7e - 0x40))):
                lbyte -= 1
            offset = HZFontInfo[section * 2 + 1]
            offset = (offset * hbyte + lbyte) * 32
#            print(hex(offset))
            offset += HZFontAddr[section]
            f.seek(offset)
            font_data = f.read(32)
            
#            for i in range(32):
#                print (hex(font_data[i]))
            for col in range(16):
                for row in range(16):
                    bit = font_data[col * 2 + row // 8] >> (7 - (row % 8)) & 0x01
                    oled.pixel(x_start + col,y_start+row,bit)
            x_start += 18
  
def Draw_HZ_Font():
    x_start = y_start = 3
    
    H_Scan = 1
    
    if H_Scan == 1:
        for row in range(16):
            for col in range(16):
                bit = fontlist[row * 2 + col // 8] >> (7 - (col % 8)) & 0x01
                oled.pixel(x_start + col,y_start+row,bit)
        x_start += 18
    else :
        for col in range(16):
            for row in range(16):
                bit = fontlist[32 + col * 2 + row // 8] >> (7 - (row % 8)) & 0x01
                oled.pixel(x_start + col,y_start+row,bit)
        x_start += 18



if __name__ == "__main__":
    
#    pin = Pin(36, Pin.OUT)
    p9 = Pin(9, Pin.IN) #配置GPIO9为输入端口
    WS2812_POWERON = 1
    WS2812_POWEROFF = 0
    ws2812_status = WS2812_POWEROFF
    offset = 0

while True:

    if (p9.value() == 0):	#是否有按键？有，切换RGB灯的状态
        ws2812_status ^= 1
        while True:
            if (p9.value() == 1):  #等待按键释放
                break
    if ws2812_status == WS2812_POWEROFF:
        np.fill((0,0,0))
        np.write()   #关闭RGB灯
    else :
        offset = ws2812_LED_On(offset)
        np.write()   #点亮RGB灯
    
    oled.fill(0x0)
    oled.text("LuoCL,I love you!",3,20,1)
    oled.rect(0,0,128,64,1)
    show_chinese("啊深圳源悦科技",3,3,1)
#    for y in range(60):
#        oled.pixel(2*y,y,1)


    oled.show()
    time.sleep(1)

    time.sleep_ms(100)


